//
//  UnConfirmFeeVC.m
//  test
//
//  Created by 李彦宏 on 2018/3/8.
//  Copyright © 2018年 朱圳. All rights reserved.
//等待确认

#import "UnConfirmFeeVC.h"
#import "UnConfHead.h"//section==0头部
#import "UnContHead.h"//section==1头部
#import "FeeDetailCell.h"//内容详情cell
#import "JWGRUsermodel.h"
#import "JWUserManager.h"
#import "JWGROtherFeemodel.h"
#import "Utility.h"
#import "KongDataView.h"

#define FeeDetailCellID @"FeeDetailCell"
@interface UnConfirmFeeVC ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *selectArry;//开合Arry
@property (nonatomic, strong) JWGRUsermodel *Gmodel;
@property (nonatomic, strong) JWGROtherFeemodel *model;
@property (nonatomic, strong) NSMutableArray *contArry;//内容

@end

@implementation UnConfirmFeeVC

//UItableView初始化
- (void)setupTable
{
    UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //隐藏组距导航栏的间距
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    tableView.backgroundColor=[UIColor whiteColor];
    tableView.showsHorizontalScrollIndicator=NO;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.dataSource=self;
    tableView.delegate=self;
tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    self.tableView.estimatedRowHeight = 120;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [tableView registerNib:[UINib nibWithNibName:@"FeeDetailCell" bundle:nil] forCellReuseIdentifier:FeeDetailCellID];
    self.tableView=tableView;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
    [EasyShowLodingView showLodingText:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setdata];
        [EasyShowLodingView hidenLoding];
    });

}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"等待确认";
    [self setupTable];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.Gmodel=[JWUserManager Gme];

    // Do any additional setup after loading the view.
}

#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.contArry.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_selectArry[section] isEqualToString:@"1"]) {
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
        FeeDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:FeeDetailCellID];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        self.model=_contArry[indexPath.section-1];
        cell.DetailTextView.text=self.model.discrabe;
        return cell;
   
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


#pragma -mark 头部的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section==0){
        UnConfHead *head1=[[UnConfHead alloc]init];
        head1.SumMoney.text=@"金额";
        return head1;
    }else{

        UnContHead *head=[[UnContHead alloc]init];
        self.model=self.contArry[section-1];
       NSString *time =[Utility getDateStringWithTimeStr:self.model.time];
       
        head.data.text=time;
        head.sum.text= self.model.money;
        if([self.model.state isEqualToString:@"0"]){
            head.IsSelet.text=@"未确认";
        }

        //添加手势
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgAction:)];
        [head addGestureRecognizer:tap];
        head.tag=section;
        return head;
    }
   
}
//手势响应
- (void)tapImgAction:(UIGestureRecognizer *)tap
{
    NSInteger index = tap.view.tag;
    
    if ([_selectArry[index] isEqualToString:@"0"]) {
        
        [_selectArry replaceObjectAtIndex:index withObject:@"1"];
        
    }else{
        [_selectArry replaceObjectAtIndex:index withObject:@"0"];
        
    }
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];
}
#pragma -mark ———————————————等待确认数据————————————————————
-(void)setdata{
    if(self.Gmodel.ID==nil){
        self.Gmodel.ID=self.Gmodel.gid;
    }
    NSDictionary *dic=@{
                         @"jwid" : JWid,
                         @"jwpassword" : JWpassword,
                         @"gid":self.Gmodel.ID,
                         @"state":@"0",
                         };
   // NSLog(@"--dic--%@", dic);
    [[AFNetworkTool shareManager]postJSONWithUrl:JWteacher_GROthFeelist parmas:dic successData:^(id json) {
        [self.contArry removeAllObjects];
        NSArray *arry=json[@"list"];
        for(int i=0;i<arry.count;i++){
    self.model=[JWGROtherFeemodel yy_modelWithJSON:arry[i]];
            [self.contArry addObject:self.model];
        }
        for(int a=0;a<arry.count+1;a++){
            [self.selectArry addObject:@"0"];
        }
        __weak typeof(self) weakslf = self;
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSInteger cout=self.contArry.count;
            
            
            if(cout==0){
                [self.tableView removeFromSuperview];
                KongDataView *konview=[KongDataView NilDataview];
                konview.frame=self.view.frame;
                [self.view addSubview:konview];
            }
            [weakslf.tableView reloadData];
        });
        
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
    
}

-(NSMutableArray *)selectArry{
    if (!_selectArry) {
        _selectArry = [[NSMutableArray alloc]init];
    }
    return _selectArry;
}
-(NSMutableArray *)contArry{
    if(_contArry==nil){
        _contArry=[[NSMutableArray alloc]init];
    }
    return _contArry;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
